package com.woniu.cms.client.core.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.cms.client.core.entity.ClientContact;
import com.woniu.cms.client.core.entity.ClientInfo;
import com.woniu.cms.client.core.entity.ClientNegotiation;
import com.woniu.cms.client.core.service.IClientContactService;
import com.woniu.cms.client.core.service.IClientInfoService;
import com.woniu.cms.client.core.vo.ClientContactVo;
import com.woniu.cms.client.core.vo.ClientInfoVo;
import com.woniu.cms.client.core.vo.ClientNegotiationVo;
import com.woniu.common.api.Result;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author chenjiapeng
 * @since 2022-09-20
 */
@RestController
@RequestMapping("/client-contact")
public class ClientContactController {


    @Autowired
    private IClientContactService iClientContactService;

    @Autowired
    private IClientInfoService iClientInfoService;

    @Transactional
    @ApiOperation("保存客户联系人的信息")
    @PostMapping
    public Result saveClientContact(@RequestBody ClientContact clientContact){

        QueryWrapper<ClientInfo> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("client_name",clientContact.getClientName());

        ClientInfo clientInfo = iClientInfoService.getOne(queryWrapper);

        clientContact.setInfoId(clientInfo.getId());

        boolean save = iClientContactService.save(clientContact);

        return save ? Result.ok() :Result.error();
    }


    @ApiOperation("根据id删除客户联系人")
    @DeleteMapping("/{id}")
    public Result deleteClientContact(@PathVariable Integer id){

        boolean b = iClientContactService.removeById(id);

        return b? Result.ok() :Result.error();

    }

    @ApiOperation("批量删除客户联系人信息")
    @DeleteMapping
    public Result deleteClientContacts(@RequestBody List<Integer> ids){
        //批量删除
        boolean b = iClientContactService.removeBatchByIds(ids);
        return b? Result.ok() :Result.error();
    }

    @Transactional
    @ApiOperation("修改客户联系人信息")
    @PutMapping
    public Result updateClientContact(@RequestBody ClientContact clientContact){


        QueryWrapper<ClientInfo> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("client_name",clientContact.getClientName());

        ClientInfo clientInfo = iClientInfoService.getOne(queryWrapper);

        clientContact.setInfoId(clientInfo.getId());

        boolean save = iClientContactService.updateById(clientContact);
        return save ? Result.ok() :Result.error();
    }


    @ApiOperation(value = "通过客户联系人姓名查找客户联系人对象")
    @GetMapping("name")
    public  Result findByName(String name) {

        System.out.println("name = " + name);

        QueryWrapper<ClientContact> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("name",name);

        ClientContact clientContact = iClientContactService.getOne(queryWrapper);

        return Result.common(clientContact);
    }

    @ApiOperation(value = "通过客户列表Id查找客户联系人对象")
    @GetMapping("/{id}")
    public  Result findById(@PathVariable  Integer id) {

        QueryWrapper<ClientContact> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("ctid",id);

        ClientContact clientContact = iClientContactService.getOne(queryWrapper);

        return Result.common(clientContact);
    }


    @GetMapping("page")
    @ApiOperation(value = "客户联系人信息的分页的查询操作")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "currentPage",value = "当前的页数",example = "1"),
            @ApiImplicitParam(name = "pageSize",value = "分页大小",example = "3"),
    })
    public Result pageClientInfo(Integer currentPage, Integer pageSize){

        IPage<ClientContact> clientContactIPage = new Page(currentPage,pageSize);

        System.out.println("clientContactIPage = " + clientContactIPage);

        IPage<ClientContact> page = iClientContactService.page(clientContactIPage);

        System.out.println("page = " + page);

        List<ClientContact> records = page.getRecords();

        System.out.println("records = " + records);


        List<ClientContactVo> clientContactVos = copyListProperties(records);


        long total = page.getTotal();

        Map<String,Object> map = new HashMap<>();
        map.put("data",clientContactVos);
        map.put("total",total);

        return Result.common(map);

    }


    private  List<ClientContactVo>  copyListProperties(List<ClientContact> records){

        List<ClientContactVo> clientContactVos  = MyBeanUtils.copyListProperties(records, ClientContactVo::new);

        return clientContactVos;

    }




}

